package com.lineage.server.world;

import com.lineage.server.model.Instance.L1PcInstance;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class WorldKnight {
  private static final Log _log = LogFactory.getLog(WorldKnight.class);
  
  private static WorldKnight _instance;
  
  private final ConcurrentHashMap<Integer, L1PcInstance> _isKnight;
  
  private Collection<L1PcInstance> _allPlayer;
  
  public static WorldKnight get() {
    if (_instance == null)
      _instance = new WorldKnight(); 
    return _instance;
  }
  
  private WorldKnight() {
    this._isKnight = new ConcurrentHashMap<>();
  }
  
  public Collection<L1PcInstance> all() {
    try {
      Collection<L1PcInstance> vs = this._allPlayer;
      return (vs != null) ? vs : (this._allPlayer = Collections.unmodifiableCollection(this._isKnight.values()));
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
      return null;
    } 
  }
  
  public ConcurrentHashMap<Integer, L1PcInstance> map() {
    return this._isKnight;
  }
  
  public void put(Integer key, L1PcInstance value) {
    try {
      this._isKnight.put(key, value);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void remove(Integer key) {
    try {
      this._isKnight.remove(key);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
}
